package com.fingard.dsp.bank.directbank.payease01;

import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

/**
 * 加解密工具类，提供标准的DES、MD5、SHA1加密实现
 * 
 * 
 */
public abstract class Des3 {
	private static final String Algorithm = "DESede"; // 定义 加密算法,可用
	// DES,DESede,Blowfish
	// keybyte为加密密钥，长度为24字节
	// src为被加密的数据缓冲区（源）
	public static byte[] encryptMode(String keybyte, byte[] src) {
		try {
			// 生成密钥
			SecretKey deskey = new SecretKeySpec(Base64.decodeBase64(keybyte.getBytes()), Algorithm);
			// 加密
			Cipher c1 = Cipher.getInstance(Algorithm);
			c1.init(Cipher.ENCRYPT_MODE, deskey);
			return Base64.encodeBase64(c1.doFinal(src));
		} catch (java.security.NoSuchAlgorithmException e1) {
			e1.printStackTrace();
		} catch (javax.crypto.NoSuchPaddingException e2) {
			e2.printStackTrace();
		} catch (java.lang.Exception e3) {
			e3.printStackTrace();
		}
		return null;
	}

	// keybyte为加密密钥，长度为24字节
	// src为加密后的缓冲区
	public static byte[] decryptMode(String keybyte, byte[] src) {
		try {
			byte[] base64Str = Base64.decodeBase64(keybyte.getBytes());
			// 生成密钥
			SecretKey deskey = new SecretKeySpec(base64Str, Algorithm);
			// 解密
			Cipher c1 = Cipher.getInstance(Algorithm);
			c1.init(Cipher.DECRYPT_MODE, deskey);
			return c1.doFinal(src);
		} catch (java.security.NoSuchAlgorithmException e1) {
			e1.printStackTrace();
		} catch (javax.crypto.NoSuchPaddingException e2) {
			e2.printStackTrace();
		} catch (java.lang.Exception e3) {
			e3.printStackTrace();
		}
		return null;
	}

	// 转换成十六进制字符串
	public static String byte2hex(byte[] b) {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1)
				hs = hs + "0" + stmp;
			else
				hs = hs + stmp;
			if (n < b.length - 1)
				hs = hs + ":";
		}
		return hs.toUpperCase();
	}
	
	

	public static void main(String[] args) {

		// 添加新安全算法,如果用JCE就要把它添加进去
		Security.addProvider(new com.sun.crypto.provider.SunJCE());
		/*final byte[] keyBytes = { 0x16, 0x44, 0x4F, 0x58, (byte) 0x81, 0x05,
				0x41, 0x01, 0x28, 0x15, 0x37, 0x53, (byte) 0xCB, (byte) 0xDD,
				0x51, 0x66, 0x77, 0x09, 0x65, (byte) 0x98, 0x30, 0x40, 0x42,
				(byte) 0xE2 };*/
		// 24字节的密钥
//		String keyBytes="ESJPWIgQQDgoJXlRy91VZncpdJgwQEDi";
//		String key = new String(Base64.encodeBase64(keyBytes));
		String key = "ESJPWIgQQDgoJXlRy91VZncpdJgwQEDi";
		System.out.println("this is key :"+key);
//		String szSrc = "This is a 3DES test. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdThis is a 3DES test. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcdtest. 测试abcabcd";
//		String szSrc = "{\"branchId\":\"\",\"groupId\":\"6978\",\"mobile\":\"13647092366\",\"reqTime\":\"20130917114202\",\"serialNum\":\"489\",\"tcFundsType\":\"0\",\"tcOrderCreateDate\":\"2013-08-09 10:56:25.0\",\"tcOrderNo\":\"20130809-6927-577079012\",\"tcSumFunds\":\"30000.00\",\"transcode\":\"TRS001001\",\"transresult\":\"2\",\"user\":\"ydwyaodawei@163.com\"}";
		String szSrc = "{\"serlNum\":\"34192200003296\",\"groupId\":\"8013\",\"transCode\":\"TRS001006\",\"reqTime\":\"2017-03-08 14:55:07\",\"operationCode\":\"18000\",\"authId\":\"\",\"user\":\"8013\",\"mobile\":\"\",\"idType\":\"\",\"id\":\"\",\"accName\":\"李尚\",\"accProvince\":\"北京\",\"accCity\":\"北京\",\"accBank\":\"中国民生银行\",\"accBranchName\":\"中国民生银行厦门文园分理处\",\"accNum\":\"0002900F0345178\",\"accType\":\"00\",\"accProp\":\"PR\",\"amount\":\"496.80\",\"branchId\":\"0\",\"merdata1\":\"\",\"merdata2\":\"\",\"channel\":\"0\",\"accBranchNum\":\"102393000420\",\"totalAmount\":\"496.80\",\"fee\":\"0.00\",\"urgency\":\"U2\",\"amountSplit\":\"1\",\"pin\":\"\",\"channelCode\":\"\",\"url\":\"\"}";
		System.out.println("加密前的字符串:" + szSrc);
//
		byte[] encoded = encryptMode(key, szSrc.getBytes());
		System.out.println("加密后的字符串:" + new String(encoded));
//
		byte[] srcBytes = decryptMode(key, Base64.decodeBase64(encoded));
		System.out.println("解密后的字符串:" + (new String(srcBytes)));
	}
}
